<?php

/**
 * 模型对应数据库表 "linkfirends".
 *
 * 以下是可用表linkfirends的栏位:
 * @property string $id
 * @property string $link_name
 * @property string $link_url
 * @property integer $link_open
 * @property string $site_key
 * @property integer $link_sort
 */
class Linkfirends extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return Linkfirends the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_linkfirends';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('link_name, link_url, link_open,is_nofollow', 'required'),
            array('link_open, link_sort, is_nofollow', 'numerical', 'integerOnly' => true),
            array('link_name, link_url', 'length', 'max' => 255),
            array('site_key', 'length', 'max' => 255),
            // The following rule is used by search(). 
            // Please remove those attributes that should not be searched. 
            array('id, link_name, link_url, link_open, site_key, link_sort, is_nofollow', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'link_name' => '链接名称',
            'link_url' => '链接URL',
            'link_open' => '打开方式',
            'link_sort' => '显示页面',
            'is_nofollow' => '权重链接',
        );
    }

    /**
     * 打开方式 
     */
    public $link_open_array = array(
        '0' => '新窗口',
        '1' => '当前窗口'
    );
    public $is_nofollow_array = array(
        '0' => '否',
        '1' => '是'
    );

    /**
     * 分类管理
     */
    public $link_sort_array = array(
        '0' => '首页',
        '1' => '全站'
    );

    /**
     * 设置查询相关条件 
     * @author nirsh 
     * @version 5.0 
     * @example 
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that 
        // should not be searched. 

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('link_name', $this->link_name, true);
        $criteria->compare('link_url', $this->link_url, true);
        $criteria->compare('link_sort', $this->link_sort);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'link_name'
                    ),
                ));
    }

}